//
//  MainTabBar.swift
//  weibo1
//
//  Created by Apple on 15/11/26.
//  Copyright © 2015年 Apple. All rights reserved.
//

import UIKit

class MainTabBar: UITabBar {
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
//        fatalError("init(coder:) has not been implemented")
        //既支持手写代码也支持sb
        super.init(coder: aDecoder)
        
    }
    
  private  func setUpUI ()
    {
        addSubview(plasBut)
    
    }
    
    //懒加载加号按钮
    lazy var plasBut :UIButton = {
        let but = UIButton()
        but.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: .Normal)
        but.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: .Highlighted)
        but.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: .Normal)
        but.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: .Highlighted)
        
        but.sizeToFit()
        return but
        }()
    
    
    
    
    
    override func layoutSubviews() {
        super.layoutSubviews()
        //设置按钮的坐标
        let w = ScreenW / 5
        let h = self.bounds.height
//        let rect = CGRectMake(0, 0, w, h)
        let rect = CGRect(x: 0, y: 0, width: w , height: h)
        var index :CGFloat = 0
        for i in subviews
        {
           //修改UITabBarButton 的frame
            if i.isKindOfClass(NSClassFromString("UITabBarButton")!)
            {
                i.frame = CGRectOffset(rect, w * index, 0)
                index += index == 1 ? 2 : 1
            }
        
        }
        plasBut.frame = CGRectOffset(rect, 2 * w, 0)
    }
    
}
